//
//  BaseTabbarVC.swift
//  newSwift
//
//  Created by MAC on 2023/10/18.
//

import UIKit
import SwifterSwift

class BaseTabbarVC : UITabBarController{
    
    open var titleLabel : UILabel?
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        prepareControllers()
        prepareTabbar()
        test()
    }
    func prepareControllers() {

        let home = BaseNavigationVC(rootViewController: HomeViewController())
        home.title = "首页"
        home.tabBarItem.title = "首页"
        home.tabBarItem.image = R.image.tab_home()
        home.tabBarItem.selectedImage = R.image.tab_home_pre()
    
        
        let plan = BaseNavigationVC(rootViewController: PlanViewController())
        plan.title = "计划"
        plan.tabBarItem.title = "计划"
        plan.tabBarItem.image = R.image.tab_plan()
        plan.tabBarItem.selectedImage = R.image.tab_plan_pre()
        
        let watch = BaseNavigationVC(rootViewController:  WatchViewController())
        watch.title = "腕表"
        watch.tabBarItem.title = "腕表"
        watch.tabBarItem.image = R.image.tab_Watches()
        watch.tabBarItem.selectedImage = R.image.tab_Watches_pre()
        
        
        let mine = BaseNavigationVC(rootViewController:  MineViewController())
        mine.title = "我的"
        mine.tabBarItem.title = "我的"
        mine.tabBarItem.image = R.image.tab_mine()
        mine.tabBarItem.selectedImage = R.image.tab_mine_pre()
        
//        let experiment = NavigationController(rootViewController: ExperimentViewController222())
//        experiment.title = "实验11"
//        experiment.tabBarItem.title = "实验11"
        
        viewControllers = [home,plan,watch,mine]
    }
    
    func prepareTabbar() {
        
        tabBar.backgroundColor = .init(hex: 0xFFFFFF)
        
        let appearance = UITabBarItemAppearance()
        appearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.init(hex: 0x333333)!]
        appearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.init(hex: 0xFF6618)!]
        let tabbarApperance = UITabBarAppearance()
//        if #available(iOS 15.0, *) {
//            tabbarApperance.configureWithOpaqueBackground()
//            tabbarApperance.backgroundColor = .white
//         }
        tabbarApperance.stackedLayoutAppearance = appearance
        tabBar.standardAppearance = tabbarApperance
        tabBar.isTranslucent = true
    }
    
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        let index = tabBar.items?.firstIndex(of: item) ?? 0
        selectAnimation(index: index)
    }
}

private extension BaseTabbarVC {
    
    func selectAnimation(index:Int){
        if let `class` = NSClassFromString("UITabBarButton") {
            let view = tabBar.subviews.filter { $0.isKind(of: `class`) }[index]
            let animation = CASpringAnimation(keyPath: "transform.scale")
            animation.mass = 0.6
            animation.stiffness = 80
            animation.damping = 10
            animation.initialVelocity = 0.5
            animation.duration = 0.35
            animation.fromValue = 0.25
            animation.toValue = 1
            animation.timingFunction = CAMediaTimingFunction(controlPoints: 0.6, 0.1, 0.30, 0.90)
            view.layer.add(animation, forKey: nil)
            
        }
    }
    
    
    func test(){
        
    }
}
